#include "visibility.h"
#include <QDebug>

//const int VISIBILITY_PROTOCOLALL_LENGTH = 97;


Visibility::Visibility(QObject* parent)
    :m_port(new QSerialPort(this))
{
    connect(this, &Visibility::protocol, this, [=](QByteArray ary){
        ary.replace(" ","");
        qDebug()<<"删除空格后的 ary : "<<ary;
        VisibilityProtocolAll protocol;
        bool ok;

        QString dataTimeStr = ary.mid(10, 14);
        int year = dataTimeStr.mid(0, 4).toInt(&ok);
        int month = dataTimeStr.mid(4, 2).toInt(&ok);
        int day = dataTimeStr.mid(6, 2).toInt(&ok);
        int hour = dataTimeStr.mid(8, 2).toInt(&ok);
        int minute = dataTimeStr.mid(10, 2).toInt(&ok);
        int second = dataTimeStr.mid(12, 2).toInt(&ok);

        protocol.date_time = QDateTime(QDate(year, month, day), QTime(hour, minute, second));


        protocol.one_minute_visibility = ary.mid(24, 5).toInt(&ok);
        protocol.ten_minute_visibility = ary.mid(29, 5).toInt(&ok);
        protocol.TheIntensityOfEmittedEnergy = ary.mid(34, 5).toInt(&ok);
        protocol.TheIntensityOfReceivedEnergy = ary.mid(39, 5).toInt(&ok);

        protocol.RatedPower = ary.mid(44, 3).toDouble(&ok)/10;
        protocol.sendPollution = ary.mid(61, 3).toDouble(&ok)/10;
        protocol.receivedPollution = ary.mid(64, 3).toDouble(&ok)/10;
        QString deviceState = ary.mid(67, 8);
        protocol.deviceState = checkDevState(deviceState);

        //发送更新显示的信号
        emit this->updateView(protocol);
        qDebug()<<"更新ui完成";
    });
}


Visibility* Visibility::instance = new Visibility();

Visibility *Visibility::getInstance()
{
    return instance;
}

bool Visibility::openDevice(QString portName)
{
    m_port->setPortName(portName);
    m_port->setBaudRate(9600);
    m_port->setDataBits(QSerialPort::Data8);
    m_port->setStopBits(QSerialPort::OneStop);
    m_port->setParity(QSerialPort::NoParity);
    m_port->setFlowControl(QSerialPort::NoFlowControl);
    m_port->setReadBufferSize(4096);
    if(m_port->open(QSerialPort::ReadWrite)){
        qDebug()<<"打开成功";
        connect(this->m_port, &QSerialPort::errorOccurred, this, [=](QSerialPort::SerialPortError err){
            qDebug()<<"错误信息 : "<<this->m_port->errorString();
        });


        connect(this->m_port, &QSerialPort::readyRead, this, [=](){
            if(this->m_port->bytesAvailable()){
                QByteArray ary = this->m_port->readAll();
                qDebug()<<"rec arr:"<<ary;
                this->m_readBuffer.append(ary);
                int index = this->m_readBuffer.indexOf("VIS");

                // |多余数据|VIS|正确数据|，删除第一次VIS前的所有数据
                if(index > 0){
                    this->m_readBuffer.remove(0, index);
                }
                if(this->m_readBuffer.length() >= VISIBILITY_PROTOCOLALL_LENGTH){
                    int index = this->m_readBuffer.indexOf("VIS");
                    qDebug()<<"index:"<<index<<"  m_readBuffer:"<<this->m_readBuffer;
                    if(index >= 0){
                        QByteArray result = this->m_readBuffer.mid(index, VISIBILITY_PROTOCOLALL_LENGTH);
                        //qDebug()<<"result : "<<result;
                        emit protocol(result);
                        this->m_readBuffer.remove(index, VISIBILITY_PROTOCOLALL_LENGTH);
                    }
                }
            }
        });
        return true;
    }
    return false;
}

void Visibility::closeDevice()
{
    m_port->close();
}

QString Visibility::checkDevState(QString DevStaStr){
    QString res = "";
    if(DevStaStr[0] == '1'){
        res.append("设备未校准或校准数据丢失\n");
    }
    if(DevStaStr[1] == '1'){
        res.append("RTC损坏\n");
    }
    if(DevStaStr[2] == '1'){
        res.append("环境温度不良\n");
    }
    if(DevStaStr[3] == '1'){
        res.append("窗口污染\n");
    }
    if(DevStaStr[4] == '1'){
        res.append("电路板温度不良\n");
    }
    if(DevStaStr[5] == '1'){
        res.append("背景光不良\n");
    }
    if(res.size() == 0){
        res.append("无");
    }
    return res;
}
